/*
 * Copyright (C) 2020-present, kinbug All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitee.kinbug.sniper.indicator;

import java.math.BigDecimal;
import java.util.List;

/**
 * -指数加权移动平均线
 * @ClassName: EMAIndicator
 * @author kinbug
 * @date 2020-12-4
 */
public class EMAIndicator {

	/**
	 * 指数加权移动平均线
	 * 算法说明：当前：EMA=ema，上一个EMA = ema1,n 周期 EMA 的平滑系数为 (2/(n+1)) = k, p是单个值
	 * 算法公式：p * k +(1-k)*ema1
	 * @param list 均数源数集合
	 */
	public static final BigDecimal getValue(final List<BigDecimal> list, final int number) {
		// 开始计算EMA值，
		BigDecimal k = new BigDecimal(2.0 / (number + 1.0));// 计算出序数
		BigDecimal ema = list.get(0);// 第一天ema等于当天收盘价
		for (int i = 1; i < list.size(); i++) {
			// 第二天以后，当天收盘 收盘价乘以系数再加上昨天EMA乘以系数-1
			ema = (list.get(i).abs().multiply(k)).add(ema.multiply((BigDecimal.ONE.subtract(k))));
		}
		return ema;
	}
	

	/**
	 * 指数加权移动平均线
	 * 算法说明：当前：EMA=ema，上一个EMA = ema1,n 周期 EMA 的平滑系数为 (2/(n+1)) = k, p是单个值
	 * 算法公式：p * k +(1-k)*ema1
	 * @param list 均数源数集合
	 */
	public static final Double getDoubleValue(final List<Double> list, final int number) {
		// 开始计算EMA值，
		double k = 2.0 / (number + 1.0);// 计算出序数
		double ema = list.get(0);// 第一天ema等于当天收盘价
		for (int i = 1; i < list.size(); i++) {
			// 第二天以后，当天收盘 收盘价乘以系数再加上昨天EMA乘以系数-1
			ema = (list.get(i)*k)+(ema*(1-k));
		}
		return ema;
	}
}
